package cn.sdormitory.smartdor.service.impl;

import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.smartdor.dao.SdDropOutDao;
import cn.sdormitory.smartdor.dao.SdRentDao;
import cn.sdormitory.smartdor.dao.SdRentPayRecordDao;
import cn.sdormitory.smartdor.dto.SdRentExportDTO;
import cn.sdormitory.smartdor.dto.SdRentImportDTO;
import cn.sdormitory.smartdor.entity.SdRent;
import cn.sdormitory.smartdor.entity.SdRentPayRecord;
import cn.sdormitory.smartdor.service.SdRentService;
import cn.sdormitory.smartdor.vo.req.SdRentFormVo;
import cn.sdormitory.smartdor.vo.req.SdRentQueryParam;
import cn.sdormitory.smartdor.vo.resp.SdRentVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SdRentServiceImpl extends ServiceImpl<SdRentDao, SdRent> implements SdRentService {

    @Autowired
    SdRentDao sdRentDao;

    @Autowired
    SdRentPayRecordDao sdRentPayRecordDao;

    @Autowired
    BStudentDao bStudentDao;

    @Autowired
    SdDropOutDao sdDropOutDao;

    @Override
    public IPage<SdRentVO> findSdRentPageList(SdRentQueryParam queryParam) {
        int count = sdRentDao.findSdRentListCount(queryParam);
        int startNum = (queryParam.getPageNum() - 1) * queryParam.getPageSize();
        List<SdRentVO> sdRentList = sdRentDao.findSdRentList(startNum, queryParam.getPageSize(), queryParam);
        IPage<SdRentVO> page = new Page<>();
        page.setTotal(count);
        page.setRecords(sdRentList);
        return page;
    }

    @Override
    public CommonResult syncStudentData() {
        QueryWrapper<BStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("status","1").eq("sync_status","0");
        List<BStudent> bStudentList = bStudentDao.selectList(wrapper);
        //用于批量插入
        List<SdRent> sdRentList = new ArrayList<>();
        List<BStudent> updateStuList = new ArrayList<>();
        if(bStudentList == null || bStudentList.size() <= 0){
            return CommonResult.success("无寄宿学生数据需要同步");
        }else {
            for(BStudent bStudent : bStudentList){
                SdRent sdRent = new SdRent();
                sdRent.setStudentNo(bStudent.getStudentNo());
                sdRentList.add(sdRent);
                bStudent.setSyncStatus("1");
                updateStuList.add(bStudent);
                bStudentDao.updateById(bStudent);
            }
            this.saveBatch(sdRentList);
            return CommonResult.success("所有寄宿学生均已同步");
        }
    }

    @Override
    public CommonResult renew(SdRentFormVo vo) throws Exception{
        if(StringUtils.isEmpty(vo.getPayAmount())){
            return CommonResult.failed("缴费金额不可为空");
        }
        if(StringUtils.isEmpty(vo.getCheckInDate())){
            return CommonResult.failed("入住日期不可为空");
        }
        if(StringUtils.isEmpty(vo.getNextPayDate())){
            return CommonResult.failed("下次缴费日期不可为空");
        }
        QueryWrapper<SdRent> wrapper = new QueryWrapper<>();
        wrapper.eq("student_no",vo.getStudentNo());
        SdRent sdRent = sdRentDao.selectOne(wrapper);
        if(sdRent == null){
            return CommonResult.failed("无此学生信息数据！请同步数据学生信息表数据！");
        }else{
            SdRentPayRecord sdRentPayRecord = new SdRentPayRecord();
            sdRentPayRecord.setStudentNo(vo.getStudentNo());
            sdRentPayRecord.setPayDate(vo.getPayDate());
            sdRentPayRecord.setCheckInDate(vo.getCheckInDate());
            sdRentPayRecord.setPayAmount(vo.getPayAmount());
            sdRentPayRecord.setNextPayDate(vo.getNextPayDate());
            sdRentPayRecordDao.insert(sdRentPayRecord);
            if(sdRent.getDeposit() == null){
                sdRent.setDeposit(vo.getDeposit());
            }
            //缴费金额不为空，状态就是"1"
            if(!("1".equals(sdRent.getPayStatus()))){
                sdRent.setPayStatus("1");
            }
            sdRentDao.updateById(sdRent);
            return CommonResult.success("缴费成功！");
        }
    }

    @Override
    public List<String> importSdSentData(List<SdRentImportDTO> sdRentDTOList) {
        List<String> failImportStu = new ArrayList<>();
        Iterator<SdRentImportDTO> iterator = sdRentDTOList.iterator();
        while (iterator.hasNext())
        {
            //读取excel中得数据如无学号，则不进行数据更新或插入
            SdRentImportDTO sdRentDTO = iterator.next();
            if(StringUtils.isEmpty(sdRentDTO.getStudentNo())){
                failImportStu.add(sdRentDTO.getStudentName());
                continue;
            }
            //更新sd_rent表
            QueryWrapper<SdRent> wrapper = new QueryWrapper<>();
            wrapper.eq("student_no",sdRentDTO.getStudentNo());
            SdRent sdRent = sdRentDao.selectOne(wrapper);
            if(sdRent == null){
                failImportStu.add(sdRentDTO.getStudentName());
                continue;
            }
            //判断是否存在押金数据
            boolean flag1 = false;
            if(StringUtils.isEmpty(sdRent.getDeposit())){
                sdRent.setDeposit(sdRentDTO.getDeposit());
                flag1 = true;
            }
            boolean flag2 = false;
            if(!("1".equals(sdRent.getPayStatus()))){
                sdRent.setPayStatus("1");
                flag2 = true;
            }
            if(flag1 || flag2){
                sdRentDao.updateById(sdRent);
            }
            //插入缴费记录表
            SdRentPayRecord sdRentPayRecord = new SdRentPayRecord();
            sdRentPayRecord.setStudentNo(sdRentDTO.getStudentNo());
            sdRentPayRecord.setPayDate(sdRentDTO.getPayDate());
            sdRentPayRecord.setCheckInDate(sdRentDTO.getCheckInDate());
            sdRentPayRecord.setPayAmount(sdRentDTO.getPayAmount());
            sdRentPayRecord.setNextPayDate(sdRentDTO.getNextPayDate());
            sdRentPayRecordDao.insert(sdRentPayRecord);
        }
        return failImportStu;
    }

    @Override
    public List<SdRentExportDTO> findSdRentExportList(SdRentQueryParam queryParam) {
        return sdRentDao.findSdRentExportList(queryParam);
    }
}
